<?php
	 echo $this->Html->css('jquery.multiselect');
	 echo $this->Html->script('jquery.multiselect');
?>
<script>
	$(function() {
		$("#xeplop").accordion();

		$( "#ngaybd" ).datepicker({
				showOtherMonths: true,
				selectOtherMonths: true,
				changeMonth: true,								
				dateFormat: "dd-mm-yy",
				showOn: "both",
				buttonImage:$('#root').val()+"img/calendar.jpg",
				buttonImageOnly: true
			});
			$( "#ngaykt" ).datepicker({
				showOtherMonths: true,
				selectOtherMonths: true,
				changeMonth: true,								
				dateFormat: "dd-mm-yy",
				showOn: "both",
				minDate: $("#ngaybd").val(),
				buttonImage:$('#root').val()+"img/calendar.jpg",
				buttonImageOnly: true
			});
			
			$("#ngaybd").change(function(){
				date = $.datepicker.parseDate('dd-mm-yy',$("#ngaybd").val());
				var nam = date.getFullYear();
				var thang = date.getMonth();
				var ngay = date.getDaysInMonth();
				var newdate = new Date(nam,thang,ngay);
				newdate = $.datepicker.formatDate('dd-mm-yy', newdate);
				$("#ngaykt").datepicker( "option", "minDate",date);
				$("#ngaykt").datepicker( "option", "maxDate",newdate);
			});

			$( "#ngaybdbhld" ).datepicker({
				showOtherMonths: true,
				selectOtherMonths: true,
				changeMonth: true,								
				dateFormat: "dd-mm-yy",
				showOn: "both",
				buttonImage:$('#root').val()+"img/calendar.jpg",
				buttonImageOnly: true
			});
			$( "#ngayktbhld" ).datepicker({
				showOtherMonths: true,
				selectOtherMonths: true,
				changeMonth: true,								
				dateFormat: "dd-mm-yy",
				showOn: "both",
				minDate: $("#ngaybd").val(),
				buttonImage:$('#root').val()+"img/calendar.jpg",
				buttonImageOnly: true
			});
			
			$("#ngaybdbhld").change(function(){
				date = $.datepicker.parseDate('dd-mm-yy',$("#ngaybdbhld").val());
				var nam = date.getFullYear();
				var thang = date.getMonth();
				var ngay = date.getDaysInMonth();
				var newdate = new Date(nam,thang,ngay);
				newdate = $.datepicker.formatDate('dd-mm-yy', newdate);
				$("#ngayktbhld").datepicker( "option", "minDate",date);
				$("#ngayktbhld").datepicker( "option", "maxDate",newdate);
			});
			
			$("#nhanvientt_id").multiselect({
				   selectedText: "đã chọn # trong # Nhân Viên",
				   checkAllText: 'Chọn Tất cả',
					uncheckAllText: 'Bỏ chọn tất cả',
				   noneSelectedText: '-- Tất cả --'
				});
			$( "#ngaybdtksl" ).datepicker({
				showOtherMonths: true,
				selectOtherMonths: true,
				changeMonth: true,								
				dateFormat: "dd-mm-yy",
				showOn: "both",
				buttonImage:$('#root').val()+"img/calendar.jpg",
				buttonImageOnly: true
			});
			$( "#ngaykttksl" ).datepicker({
				showOtherMonths: true,
				selectOtherMonths: true,
				changeMonth: true,								
				dateFormat: "dd-mm-yy",
				showOn: "both",
				buttonImage:$('#root').val()+"img/calendar.jpg",
				buttonImageOnly: true
			});
			$("#ngaybdtksl").change(function(){
				date = $.datepicker.parseDate('dd-mm-yy',$("#ngaybdtksl").val());
				$("#ngaykttksl").datepicker( "option", "minDate",date);				
			});
			$("#kenh_id").multiselect({
				   selectedText: "đã chọn # trong # Kênh",
				   checkAllText: 'Chọn Tất cả',
					uncheckAllText: 'Bỏ chọn tất cả',
				   noneSelectedText: '-- Tất cả --'
				});
			$("#hanghoa_id").multiselect({
				   selectedText: "đã chọn # trong # Chỉ tiêu",
				   checkAllText: 'Chọn Tất cả',
					uncheckAllText: 'Bỏ chọn tất cả',
				   noneSelectedText: '-- Tất cả --'
				});
			$("#nhanvientk_id").multiselect({
				   selectedText: "đã chọn # trong # Nhân Viên",
				   checkAllText: 'Chọn Tất cả',
					uncheckAllText: 'Bỏ chọn tất cả',
				   noneSelectedText: '-- Tất cả --'
				});
			
});
</script>
<?php 
	echo $form->create('Kqbanhangtt');
	echo $form->input('root',array('id'=>'root','type'=>'hidden','value'=>$this->webroot));
	echo $form->input('donvi_id', array('id'=>'donvi_id','label'=>'Đơn vị','options'=>$donvis,'selected'=>$donvi_id));
?>
<div id='xeplop'>
	
	<h3><a href='#'>Lũy Kế Đơn Vị</a></h3>
	<div id='luyke'>
		<div id='lkdv'>
			<?php
				if (!empty($loaikenhdvs) && !empty($chitieudvs) && !empty($datadvsl))
				{
					echo "<h3 align=center>Kết Quả Bán Hàng Theo Chỉ Tiêu</h3>";
					echo "<p align=center><b>".$donvi[0]['Donvi']['tendonvi']."</b></p><br>";
				    echo $this->Html->link(__('Xuất file Excel', true), 
				            array('action' => 'laykqbhexcel',$donvi_id,$ngaybd,$ngaykt));
					echo $ctable->show($loaikenhdvs, $chitieudvs, $datadvsl,1);
				}
				else 
					echo "<h3>Không có dữ liệu</h3>";
				
			?>
		</div>
	</div>
	
	<h3><a href='#'>Kết Quả Bán hàng Tháng - Ngày - Tuần</a></h3>
	<div id='kqbanhang_thang_tuan_ngay'>
		<?php
			echo "<h2 align=center>Chọn thời gian để xem kết quả, lưu ý khoảng thời gian này phải nằm trong thời gian giao chỉ tiêu</h2>";
		?>
		<table>
		<tr>
			<td>
				<?php echo $form->input('ngaybd',array('id'=>'ngaybd','type'=>'text','label'=>'Ngày Bắt Đầu','value'=>date('d-m-Y',strtotime($ngaybdngay))));?>
			</td>
			<td>
				<?php echo $form->input('ngaykt',array('id'=>'ngaykt','type'=>'text','label'=>'Ngày Kết Thúc','value'=>date('d-m-Y',strtotime($ngayktngay))));?>
			</td>
			<td>
				<?php echo $js->submit('  Xem  ', array('url'=> array('controller'=>'kqbanhangtts', 'action'=>'laykqbhtt'), 
    												'update' => '#kqbh'));?>
			</td>
		</tr>
        	
    	</table> 
		<div id='kqbh'>
			<?php
				if (!empty($loaikenhdvngays) && !empty($chitieudvngays) && !empty($datadvslngay))
				{
					echo "<h3 align=center>Kết Quả Bán Hàng Theo Chỉ Tiêu</h3>";
					echo "<p align=center><b>".$donvi[0]['Donvi']['tendonvi']."</b></p><br>";
				    echo $this->Html->link(__('Xuất Excel', true), 
				            array('action' => 'laykqbhexcel',$donvi_id,$ngaybdngay,$ngayktngay));
					echo $ctable->show($loaikenhdvngays, $chitieudvngays, $datadvslngay,1);
				}
				else 
					echo "<h3>Không có dữ liệu</h3>";
				
			?>
		</div>
	</div>
	
	<h3><a href='#'>Lũy Kế bán Hàng Của Nhân viên</a></h3>
	<div id='luy_ke_nhan_vien' >
		<div id='lknv'>
			<?php
				if (!empty($cols) && !empty($rows) && !empty($nhanviens) && !empty($nhanviens))
				{
					echo "<h2>Kết quả tổng hợp lũy kế của nhân viên</h2>";
				    echo $this->Html->link(__('Xuất Excel', true), array('controller'=>'kqbanhangs','action' => 'luyketonghopexcel',$donvi_id));
				
					echo $ctable->showtable_lk('Kqbanhangtt', $cols, $rows, $nhanviens, $luyketh,$tong);
				}
				else
					echo "<h2>Không Có Dữ Liệu</h2>"; 
			?>
		</div>
	</div>
<!--	
	<h3><a href='#'>Kết Quả Bán Hàng Lưu động</a></h3>
	<div id='ketquaBHLD'>
			<table>
		  <tr>
		    <th><?php echo $form->input('ngaybdbhld',array('id'=>'ngaybdbhld','type'=>'text','label'=>'Từ','value'=>date('d-m-Y')));?></th>
		    <th><?php echo $form->input('ngayktbhld',array('id'=>'ngayktbhld','type'=>'text','label'=>'Đến','value'=>date('d-m-Y')));?></th>
		   </tr>
		   <tr>
		   <th><?php echo $form->input('nhanvientt_id',array('id'=>'nhanvientt_id','multiple'=>"multiple",'label'=>'Nhân viên','options'=>$nhanvientt));?></th>
		  
		  	<th><?php echo $js->submit('  Xem  ', array(
										'url'=> array('controller'=>'kqbanhangtts','action'=>'layketqua'),
		 								'update' => '#kqbhld'));?></th>
		  </tr>
		</table>

		<div id='kqbhld'>
		<?php if(!empty($hanghoabhlds) && !empty($chitieungaybhlds)) {?>
			<table id ='tables'>
				<tr>
				<th rowspan=2>STT</th>
				<th rowspan=2>NVDN</th>
				<th rowspan=2>Đội</th>
				<th rowspan=2>Địa Điểm</th>
				<th rowspan=2>Ngày</th>
				<th rowspan=2>Thành viên tham gia</th>
				<th colspan=<?php echo count($hanghoabhlds)?>>Chỉ tiêu</th>
				<th rowspan=2>Ghi chú</th>
				</tr>
				<tr>
				<?php foreach ($hanghoabhlds as $keyhh =>$valhh):?>
				<th><?php echo $valhh ?></th>
				<?php endforeach;?>
				</tr>
				<?php
					$i=0; 
					 foreach ($chitieungaybhlds as $chitieungay):
					  $i++;
				?>
				<tr>
				<td><?php echo $i?></td>
				<td><?php echo $chitieungay['Nhanvien']['tennv']?></td>
				<td><?php echo $chitieungay['Kehoachbhld']['tendoi']?></td>
				<td><?php echo $chitieungay['Kehoachbhld']['diadiem']?></td>
				<td><?php echo date('d-m-Y',strtotime($chitieungay['Kehoachbhld']['thoigian']))?></td>
				<td><?php echo $chitieungay['Kehoachbhld']['nhansu']?></td>
				<?php foreach ($hanghoabhlds as $keyhh =>$valhh):?>
				<td>
				<?php
					foreach ($chitieungay['Chitieungay'] as $ctngay)
					{
						if($ctngay['hanghoa_id']==$keyhh)
				 			echo $ctngay['ketqua'];
					}
				?>
				</td>
				<?php endforeach;?>
				<td><?php echo $chitieungay['Kehoachbhld']['ghichu']?></td>
				</tr>
				<?php endforeach;?> 
				</table>
					<p>
					<?php
					echo $this->Paginator->counter(array(
					'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%', true)
					));
					?>	</p>
				
					<div class="paging">
						<?php echo $this->Paginator->prev('<< ' . __('previous', true), array(), null, array('class'=>'disabled'));?>
					 | 	<?php echo $this->Paginator->numbers();?>
				 |
						<?php echo $this->Paginator->next(__('next', true) . ' >>', array(), null, array('class' => 'disabled'));?>
					</div>
					<?php }?>
		</div>
	</div>
	
	<h3><a href=#>Thống kê sản phẩm theo kênh</a></h3>
		<div id='thongke_soluong'>
			<table>
				<tr>
					<td><?php echo $form->input('ngaybdtksl',array('label'=>'Từ','id'=>'ngaybdtksl','value'=>date('d-m-Y',strtotime($ngaybdtk))))?></td>
					<td><?php echo $form->input('ngaykttksl',array('label'=>'Đến','id'=>'ngaykttksl','value'=>date('d-m-Y',strtotime($ngaykttk))))?></td>
				</tr>
				<tr>
					<td><div id='nhanvientk'><?php echo $form->input('nhanvientk_id',array('id'=>'nhanvientk_id','multiple'=>"multiple",'label'=>'Nhân viên','options'=>$nhanvientt));?></div></td>
					<td><div id='kenh'><?php echo $form->input('kenh_id',array('id'=>'kenh_id','label'=>'Kênh','multiple'=>"multiple",'options'=>$kenhtks))?></div></td>
				</tr>
				<tr>
					<td><?php echo $form->input('hanghoa_id',array('id'=>'hanghoa_id','label'=>'Chỉ tiêu','multiple'=>"multiple",'options'=>$hhtks))?></td>
					<td align='center'>
						<?php echo $js->submit('  Thực hiện  ', array(
														'url'=> array('controller'=>'kqbanhangtts', 'action'=>'laytksoluongdv'),
						 								'update' => '#kqtksl'));
						?>
					</td>
				</tr>
				</table>
			<div id='kqtksl'>
			<?php
					if(!empty($kenhtks) && !empty($hhtks) && !empty($datatk))
						echo $ctable->show($hhtks, $kenhtks, $datatk); 
			?>
			</div>
		</div>
-->
</div>

<?php
$this->Js->get('#donvi_id')->event('change',
							$this->Js->request(array('controller' => 'kqbanhangtts', 'action' => 'layluykett'),
							array('update' => '#lkdv','async' => true,
							'dataExpression' => true,'method' => 'post',
							'data' => $js->serializeForm(array('isForm' => false, 'inline' => true)))));

$this->Js->get('#donvi_id')->event('change',
							$this->Js->request(array('controller' => 'kqbanhangs', 'action' => 'emptykq'),
							array('update' => '#kqbh','async' => true,
							'dataExpression' => true,'method' => 'post',
							'data' => $js->serializeForm(array('isForm' => false, 'inline' => true)))));
							
$this->Js->get('#donvi_id')->event('change',
							$this->Js->request(array('controller' => 'kqbanhangs', 'action' => 'emptykq'),
							array('update' => '#kqbhld','async' => true,
							'dataExpression' => true,'method' => 'post',
							'data' => $js->serializeForm(array('isForm' => false, 'inline' => true)))));
							
$this->Js->get('#donvi_id')->event('change',
							$this->Js->request(array('controller' => 'kqbanhangtts', 'action' => 'luyketonghopnv'),
							array('update' => '#lknv','async' => true,
							'dataExpression' => true,'method' => 'post',
							'data' => $js->serializeForm(array('isForm' => false, 'inline' => true)))));
$this->Js->get('#donvi_id')->event('change',
							$this->Js->request(array('controller' => 'kqbanhangtts', 'action' => 'laynvtk'),
							array('update' => '#nhanvientk','async' => true,
							'dataExpression' => true,'method' => 'post',
							'data' => $js->serializeForm(array('isForm' => false, 'inline' => true)))));
							
							
$this->Js->get('#nhanvientk')->event('change',
							$this->Js->request(array('controller' => 'kqbanhangtts', 'action' => 'laykkenh'),
							array('update' => '#kenh','async' => true,
							'dataExpression' => true,'method' => 'post',
							'data' => $js->serializeForm(array('isForm' => false, 'inline' => true)))));

?>